﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using FimetContoDeposito.BLL;
using FimetContoDeposito.BLL.Facades;
using FimetContoDeposito.BLL.Metadata;
using FimetContoDeposito.BLL.Objects;
using FimetContoDeposito.BLL.ResultSets;
using FimetContoDeposito.DAL;
using GrayParrot.Core;
using GrayParrot.Core.Json;
using GrayParrot.Web;
using GrayParrot.Web.Controls;
using GrayParrot.Web.Formly;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace FimetContoDeposito.Controllers
{
    public class MagazzinoController : ApiController
    {
        [HttpGet]
        public string LoadFieldsReportMagazzino()
        {
            string retValue = null;

            var m_fields = FormlyHelper.ToFormly<AnagraficaMagazziniReportMetadata>(typeof(AnagraficaMagazziniReportMetadata));

            JsonSerializerSettings settings = new JsonSerializerSettings();
            settings.ContractResolver = new NullToEmptyListResolver();
            settings.Converters = new List<JsonConverter> { new Newtonsoft.Json.Converters.StringEnumConverter { CamelCaseText = false } };
            settings.Formatting = Formatting.Indented;
            settings.NullValueHandling = NullValueHandling.Ignore;

            retValue = JsonConvert.SerializeObject(m_fields, settings);

            return retValue;
        }

        [HttpPost]
        public System.Net.Http.HttpResponseMessage Load(MagazzinoParams data)
        {
            System.Net.Http.HttpResponseMessage retValue = new HttpResponseMessage(HttpStatusCode.InternalServerError);

            Guid m_idcliente = Guid.Parse(FormlyExtensions.SanitizeDropdownItem(data.IdCliente));

            using (MagazzinoFacade m_dc = MagazzinoFacade.Create())
            {
                ReturnValue<IQueryable<SituazioneMagazzino>> m_objs = m_dc.GetSituazioneMagazzino(m_idcliente);

                DTResult<SituazioneMagazzino> result = DTResult<SituazioneMagazzino>.Empty("Nessun elemento trovato");
                if (m_objs.HasValue)
                {
                    MagazzinoResultSet m_rs = new MagazzinoResultSet(data, m_objs.Value);
                    result = m_rs.GetResult();
                }
                else
                {
                    if (m_objs.HasError)
                    {
                        result.Message.MessageType = MessageTypes.Error;
                        result.Message.Message = m_objs.Error.Message;
                    }
                }

                retValue = Request.CreateResponse(HttpStatusCode.OK, result);
            }

            return retValue;
        }

        //[HttpPost]
        //public List<NameValue<string, Guid>> LoadMagazzini(JObject data)
        //{
        //    List<NameValue<string, Guid>> retValue = null;

        //    dynamic json = data;
        //    Guid m_id = Guid.Parse(json.parameters[0].ToString());

        //    using (MagazziniFacade m_cf = MagazziniFacade.Create())
        //    {
        //        var objs = m_cf.GetAllByArticolo(m_id);

        //        if (objs.HasValue)
        //        {
        //            if (objs.Value.Any())
        //            {
        //                retValue = objs.Value.Select(x => new NameValue<string, Guid>() { name = x.Nome, value = x.Id }).ToList();
        //                retValue.Insert(0, new NameValue<string, Guid>() { name="Seleziona un elemento...", value = Guid.Empty});
        //            }
        //        }
        //    }

        //    return retValue;
        //}

        [HttpPost]
        public System.Net.Http.HttpResponseMessage CalcolaGiacenza(JObject data)
        {
            System.Net.Http.HttpResponseMessage retValue = null;

            dynamic json = data;

            Guid m_id = Guid.Parse(json.id.ToString());

            using (MagazzinoFacade m_cf = MagazzinoFacade.Create())
            {
                ReturnValue<decimal> result = m_cf.GetGiacenza(m_id);

                retValue = Request.CreateResponse(HttpStatusCode.OK, result.Value);
            }

            return retValue;
        }

        [HttpPost]
        public System.Net.Http.HttpResponseMessage CalcolaQuantitaArticolo(JObject data)
        {
            System.Net.Http.HttpResponseMessage retValue = null;

            dynamic json = data;

            Guid m_id = Guid.Parse(json.id.ToString());

            using (MagazzinoFacade m_cf = MagazzinoFacade.Create())
            {
                ReturnValue<FCD_CalcolaQuantitaReturnModel> result = m_cf.GetQuantitaArticolo(m_id);

                retValue = Request.CreateResponse(HttpStatusCode.OK, result.Value);
            }

            return retValue;
        }

        [HttpPost]
        public List<NameValue<string, Guid>> LoadMagazziniByCustomer(JObject data)
        {
            List<NameValue<string, Guid>> retValue = null;

            dynamic json = data;
            Guid m_id = Guid.Parse(json.parameters[0].ToString());

            using (MagazziniFacade m_cf = MagazziniFacade.Create())
            {
                var objs = m_cf.GetAllByCustomer(m_id);

                if (objs.HasValue)
                {
                    if (objs.Value.Any())
                    {
                        retValue = objs.Value.Select(x => new NameValue<string, Guid>() { name = x.Nome, value = x.Id }).ToList();
                        retValue.Insert(0, new NameValue<string, Guid>() { name = "Seleziona un elemento...", value = Guid.Empty });
                    }
                }
            }

            return retValue;
        }
    }
}